package servlet;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/test")
public class TestServlet extends HttpServlet {

    @Override
    public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

        System.out.println("test");
//        获取请求的提交方式
        String method = req.getMethod();
//        获取请求的uri
        req.getRequestURI();
//        获取请求的url
        req.getRequestURL();
//        获取协议和协议版本
//        req.getScheme(),req.getProtocol();

//        基于某个名称的请求头去获取该头对应的值,根据k获取v
        String V = req.getHeader("K");
//        获取传递的参数
        //根据参数K获取v
        String username = req.getParameter("username");
        String pwd = req.getParameter("pwd");
        System.out.println(username+"  "+pwd);

        //解决响应时的中文乱码
        res.setCharacterEncoding("utf-8");
        res.setContentType("text/html;charset=utf-8");

        //响应文本
        //write参数只能是字符串 int 字符数组
        PrintWriter writer = res.getWriter();
        writer.write("只能写 String int String[]");
        writer.write("<h1>测试<h1>");
        writer.print("整数，数字，Object等都可以写");

    }
}
